1. What is this?

NetworkX is a python module and so requires at least partial understanding of the python programming language. Python is a dynamic, imperative language. This means there is no compilation, or any extra steps to make your code work. You can just type in your commands and things will just work.

We are going to use something called Jupyter Notebook, as it makes research easily understood and reproducible. This notebook will serve as a quick introduction to Python and Jupyter notebook.

For example, to print out the words Hello, World, all we have to do is enter the following code, and execute it. To execute code click in the box below (called a cell) and hold the Shift key and press Enter.


In [ ]:
print("Hello, World")

Now let's try to print something else. Instead of having python print Hello, World, have it print your name! Click in the box and type the code to do that.


In [ ]:

The notebook contains cells which you can enter code into. To make a new cell below this one, click on this text, then hit the ESC key, then hit the 'b' button on your keyboard.

Anytime you want to insert a new cell, you can click somewhere, hit the escape key, then hit 'b' to insert a new cell below, or 'a' to put a new cell above. You can double click this text to edit it. Execute all the code cells that are already in the notebook as you go!

2. Python Basics

Python is a very easy language to learn. If you want to store some information in a variable, you use the equals sign (=). Execute the cell below (click in it, hold shift, and press enter).


In [ ]:
x = 5

Now, whenever python sees 'x' it will know that you mean 5


In [ ]:
print(x)

Jupyter Notebook is special in that you don't actually have to type 'print' every time. If you just end a cell with a variable, it will tell you the value of that variable.


In [ ]:
x

Python has many standard features you'd find in a regular programming language. You can add things together


In [ ]:
x+12

You can multiply, subtract and divide as well! Try each below!


In [ ]:

You declare strings with double quotes ("") or single quotes ('')


In [ ]:
s = "Parrot"
print(s)

Python has builtin support for lists. You can make lists with square brackets ([]).


In [ ]:
L = ["Cheese","Parrot","Spam","Sir Robin","Ni!"]
print(L)

You can access elements in a list using square brackets. Inside the square brackets you can put the number of the element you want to access (starting with 0).


In [ ]:
L[2]

Try printing out the last value in the list using indexing. Remember you have to start counting from 0!


In [ ]:

Last python has a data structure called a dictionary. This is their implementation of a hash table. It is similar to a list except that it is indexed by other objects (keys). They are efficient and NetworkX makes extensive use of them. This are made using curly braces({}) and colons(:). They associate a name with an item. They can be useful for storing properties about an object. You can add things to dictionaries after you create them.


In [ ]:
d = {"Name":"Arthur",
     "Occupation":"King of the Britons",
     "Squire":"Patsy",
     "Number of Subjects":1250238}
print(d["Name"])
d["Sword"] = "Excalibur"

A. If statements

If statements allow your code to make decisions, for example:


In [ ]:
x = 5
if x > 10: # Remember the '>' symbol means greater than
    print("SOOO BIG!")
else:
    print("Not so big")

Copy the code above, into the cell below. Change it so it prints "SOOO BIG!". Change it in another way so it does something different


In [ ]:

B. Loops

Sometimes we want to do things repeatedly. The best way to do that is with loops.


In [ ]:
for name in ["Bob","Bill","Helga","Joe","Mike","Brenda"]:
    print("Hello, "+name+ "!")

Copy the code above. At a variable i=0 before the loop. Inside the loop make it so 1 is added to i each time through, and print i.


In [ ]:

More Python

There is lots more to learn, but we need to get onto our network analysis. If you are interested in learning more python I recommend Learn Python the Hard Way. Don't worry it's not actually that hard!

Jupyter Notebook

Jupyter notebook has some special features that can be particularly helpful. For example it has built in completion. Let's import a module and check it out.


In [ ]:
import numpy as np

Now you can view all the functions in numpy by typing np.[TAB], that is type np. and then hit the tab key


In [ ]:
np.

If you are looking for a particular function you can just start typing the name. Let's say we are looking for the shortest path functions in NetworkX. We would import NetworkX


In [ ]:
import networkx as nx

Then we can type nx.sho[TAB] and it will list all the functions that start with sho in the networkx module


In [ ]:
nx.sho

One of the best features of Jupyter notebook is the ability to read documentation. You can do this by adding a ? to modules, classes, or functions.


In [ ]:
nx.betweenness_centrality?

If you want to view the code for the function you can use ??


In [ ]:
nx.gnp_random_graph??

If you'd like to learn more about the nobook check out the notebook docs here. But this should be enough to get us started with NetworkX.


In [ ]: